<template>
  <el-drawer v-model="drawerVisible" :destroy-on-close="true" size="900px" :title="`${drawerProps.title}权限节点`">
    <el-form
      ref="roleFormRef"
      label-width="150px"
      label-suffix=" :"
      :rules="rules"
      :disabled="drawerProps.isView"
      :model="item"
      :hide-required-asterisk="drawerProps.isView"
    >
      <el-form-item label="角色名称" prop="name">
        <el-input v-model="item.name" placeholder="请填写角色名称" clearable></el-input>
      </el-form-item>
      <el-form-item label="是否启用" prop="status">
        <el-radio-group v-model="item.status">
          <el-radio :value="1">是</el-radio>
          <el-radio :value="0">否</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="权限节点" prop="rightsList">
        <el-cascader
          v-model="item.rightsListArr"
          :options="menuData"
          :props="{ value: 'rightKey', label: 'name', multiple: true }"
          @change="handleChange"
          placeholder="请选择权限节点"
          clearable
          size="large"
        ></el-cascader>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="drawerVisible = false">取消</el-button>
      <el-button v-show="!drawerProps.isView" type="primary" @click="handleSubmit">确定</el-button>
    </template>
  </el-drawer>
</template>

<script setup lang="ts" name="RoleEdit">
import { ref, reactive, onUpdated } from "vue";
import { ElMessage, FormInstance } from "element-plus";
import { System } from "@/api/interface";
import { getSystemResourceTree, getAdminRoleInfo } from "@/api/modules/system";

let item = ref<System.AdminRoleInfo>({
  id: 0,
  name: "",
  status: 1,
  rightsList: "",
  rightsListArr: []
});
let menuData = ref();
onUpdated(() => {
  const menuSelect = getSystemResourceTree({ isMenu: "" });
  menuSelect.then(res => {
    menuData.value = res.data;
  });
  if (drawerProps.value.title == "编辑") {
    const adminRole = getAdminRoleInfo({ id: drawerProps.value.row.id });
    adminRole.then(res => {
      item.value = res.data;
      item.value.rightsListArr = item.value.rightsList.split(",");
    });
  } else {
    item.value = { id: 0, name: "", status: 1, rightsList: "", rightsListArr: [] };
  }
});
//二维数组转字符串
const arrayToString = arr => {
  return arr.map(subArr => subArr.join(",")).join(",");
};
const handleChange = value => {
  item.value.rightsList = arrayToString(value);
};

interface DrawerProps {
  title: string;
  isView: boolean;
  row: Partial<System.AdminRole>;
  api?: (params: any) => Promise<any>;
  getTableList?: () => void;
}

const drawerVisible = ref(false);
const drawerProps = ref<DrawerProps>({
  isView: false,
  title: "",
  row: {}
});

// 接收父组件传过来的参数
const acceptParams = (params: DrawerProps) => {
  drawerProps.value = params;
  drawerVisible.value = true;
};
const rules = reactive({
  name: [{ required: true, message: "请填写角色名称" }],
  status: [{ required: true, message: "请选择是否启用" }],
  rightsList: [{ required: true, message: "请选择权限节点" }]
});
// 提交数据（新增/编辑）
const roleFormRef = ref<FormInstance>();
const handleSubmit = () => {
  roleFormRef.value!.validate(async valid => {
    if (!valid) return;
    try {
      await drawerProps.value.api!(item.value);
      ElMessage.success({ message: `${drawerProps.value.title}角色成功！` });
      drawerProps.value.getTableList!();
      drawerVisible.value = false;
    } catch (error) {
      console.log(error);
    }
  });
};

defineExpose({
  acceptParams
});
</script>
